#include "CBuildMap.h"

CBuildMap::CBuildMap()
{
    //ctor
    m_mark = 0;
}

CBuildMap::~CBuildMap()
{
    //dtor
    DELPOINT(m_shape);
}

int CBuildMap::GetMark()
{
    return m_mark;
}

void CBuildMap::ShowOption()
{
    cout<<"The option of size"<<endl;
    cout<<"1 --> length=3 width=2"<<endl;
    cout<<"2 --> length=5 width=3"<<endl;
    cout<<"3 --> length=9 width=7"<<endl;

    cout<<"The option of shape"<<endl;
    cout<<"1 --> Number"<<endl;
    cout<<"2 --> Alpha"<<endl;
    cout<<"3 --> Graph"<<endl;

    cout<<"Now, please input your selection by the order of size and shape"<<endl;
}

void CBuildMap::RunGame(int _level,int _shapeType)
{
    init();
    m_level = _level;
    m_shapeType = _shapeType;

    switch (m_level)
    {
        case 1: m_length = 3;
                m_width = 2;
                break;
        case 2: m_length = 5;
                m_width = 3;
                break;
        case 3: m_length = 9;
                m_width = 7;
                break;
        default:
                cout<<"Select size error"<<endl;
    }

    switch (m_shapeType)
    {
        case 1: this->m_shape = new CShapeNum;
                break;
        case 2: this->m_shape = new CShapeAlpha;
                break;
        case 3: this->m_shape = new CShapeGraph;
                break;
        default:
                cout<<"Select shape type error"<<endl;
    }
    BuildMap();
    ShowMap();
    InputMemory();
}

void CBuildMap::init()
{
    m_mark = 0;
    this->m_shape = NULL;
    m_length = 0;
    m_width = 0;
}

void CBuildMap::BuildMap()
{
    cout<<"The game is building in progress, please wait"<<endl;
    m_map.resize (m_length);
    unsigned int i,j;
    for (i=0;i<m_map.size();i++)
        m_map[i].resize(m_width);

    for (i=0;i<m_length;i++)
    {
        for (j=0;j<m_width;j++)
        {
            m_map[i][j]=(char)(this->m_shape->GetRandom());
            sleep(1);
        }
    }
}

void CBuildMap::ShowMap()
{
    system("clear");
    cout<<"Now , the game is beginning,"<<endl;
    cout<<"the map of shape will be on the screen for 5 seconds"<<endl<<endl<<endl;
    unsigned int i,j;
    for (i=0;i<m_length;i++)
    {
        for (j=0;j<m_width;j++)
        {
            cout<<"  "<<m_map[i][j]<<' ';
        }
        cout<<endl<<endl;
    }
    cout<<endl<<endl<<endl;
    cout<<"Please don't input anything"<<endl;
    sleep(5);
    system("clear");
}

void CBuildMap::InputMemory()
{
    cout<<"Now, please input your answer "<<endl;
    unsigned int i,j;
    char tmp;
    for (i=0;i<m_length;i++)
    {
        for (j=0;j<m_width;j++)
        {
            cin>>tmp;
            if (tmp == m_map[i][j])
                m_mark++;
        }
    }
    system("clear");
}
